<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/img/home_avatar.jpeg" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout" plain>退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="switchColl">|||</div>
        <el-menu
          class="el-menu-vertical"
          background-color="#c6e2ff"
          text-color="#333"
          active-text-color="#409EFF"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath">
          <!--   一级菜单   -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--   二级菜单   -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children"
                          :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      isCollapse: false,
      menulist: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-key',
        101: 'el-icon-shopping-bag-2',
        102: 'el-icon-document-copy',
        145: 'el-icon-data-analysis'
      },
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.menulist = res.data
    },
    switchColl () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (actPath) {
      window.sessionStorage.setItem('activePath', actPath)
      this.activePath = actPath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #409EFF;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: #eee;
  }
}
.el-aside {
  background-color: #c6e2ff;
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-main {
  background-color: #ecf5ff;
}
.toggle-button {
  font-size: 15px;
  background-color: #d9ecff;
  color: #2b4b6b;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
